using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Sockets;

using IGlobalist.Net.Rtp;

namespace IGlobalist.LectureCastService
{
    public partial class LectureCastService : ServiceBase
    {
        private RtpSession serverSession = null;
        public LectureCastService()
        {
            InitializeComponent();
        }

         

        protected override void OnStart(string[] args)
        {
            int port = 12000;
            IPAddress addr = GetIP();
            if (args.Length != 0)
            {
                addr = IPAddress.Parse(args[0]);
                if(args.Length>1)
                    port = Convert.ToInt32(args[1]);
            }
            IPEndPoint ep = new IPEndPoint(addr, port);
            IPEndPoint fakeEp = new IPEndPoint(IPAddress.Loopback, port);
            RtpParticipant part = new RtpParticipant("connect-point", "connect-point",false, true, true,ep);
            serverSession = new RtpSession(ep, fakeEp, part, true, true,true);
        }

        protected override void OnStop()
        {
            serverSession.Dispose();
            serverSession = null;
        }
        protected override void OnCustomCommand(int command)
        {
            base.OnCustomCommand(command);
        }


        private static IPAddress GetIP()
        {
            foreach (IPAddress a in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (!IPAddress.IsLoopback(a)&&a.AddressFamily == AddressFamily.InterNetwork)
                {
                    return a;            
                }
            }
            return null;
        }
    }
}
